package cn.acewill.yxd.yunxundian.utils;

import android.app.Activity;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by XXX on 2017/8/30.
 */

public class CacheActivity{

        private static List<Activity> activityList = new LinkedList<>();

        public CacheActivity() {

        }

        /**
         * 添加到Activity容器中
         */
        public static void addActivity(Activity activity) {
            if (!activityList.contains(activity)) {
                activityList.add(activity);
            }
        }

        /**
         * 便利所有Activigty并finish
         */
        public static void finishActivity() {
            for (Activity activity : activityList) {
                activity.finish();
            }
            //清空数据
            if (activityList!=null){
                activityList.clear();
            }
        }

        /**
         * 结束指定的Activity
         */
        private static void finishSingleActivity(Activity activity) {
            if (activity != null) {
                if (activityList.contains(activity)) {
                    activityList.remove(activity);
                }
                activity.finish();
                activity = null;
            }
        }

        /**
         * 结束指定类名的Activity 在遍历一个列表的时候不能执行删除操作，所有我们先记住要删除的对象，遍历之后才去删除。
         */
        public static void finishSingleActivityByClass(Class<?> cls) {
            Activity tempActivity = null;
            for (Activity activity : activityList) {
                if (activity.getClass().equals(cls)) {
                    tempActivity = activity;
                }
            }
            finishSingleActivity(tempActivity);
        }
}
